<?php

namespace App\Jobs\goods;

use App\Models\config\Config;
use App\Models\fund\Coupon;
use App\Models\fund\Rebate;
use App\Models\goods\Order;
use App\Models\goods\OrderDetail;
use App\Models\log\ErrorLog;
use App\Models\User;
use App\Models\user\UserSuperior;
use App\Services\common\SmsService;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

class SeckillReward implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * 任务最大尝试次数。
     *
     * @var int
     */
    public $tries = 5;

    protected $user;


    protected $order;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct( $user, $order)
    {
        $this->user = $user;
        $this->order = $order;
    }

    /**
     *
     * 秒杀产品购买返点
     *
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $service = new SmsService();
        try{
            $map['user_id'] = $this->user->id;

            $rows = UserSuperior::where($map)->limit(2)->orderBy('level')->get();

            foreach ($rows as $k => $v){
                if ($v instanceof UserSuperior);
                if ($k == 0){
                    $lv = 1;
                    $ratio = "sp1-ratio";
                }else{
                    $lv = 2;
                    $ratio = "sp2-ratio";
                }

                //给对应用户返点
                $user = User::find($v['superior_id']);

                if ($user->level > 3){//大于3级才享受返点

                    //计算返点金额
                    $money = bcmul($this->order->total_price,Config::getConfig($ratio),2);
                    $this->order->rebate_money += $money;
                    $this->order->order->increment('rebate_money',$money);
                    $item['user_id'] = $user->id;
                    $item['relation_user_id'] = $this->user->id;
                    $item['order_id'] = $this->order->order_id;
                    $item['order_detail_id'] = $this->order->id;
                    $item['type'] = 1;
                    $item['amount'] = $money;
                    $item['title'] = "用户[{$this->user->name}]购买[{$this->order->goods->name}]奖励";
                    $item['lv'] = 3;
                    $item['money_type'] = 1;
                    $item['status'] = 0;
                    Rebate::create($item);
                    $v->rebate += $money;
                    $v->save();
                    //获得奖励发送消息
                    $service->sendSms($user->phone,SmsService::ORDER_CODE,['code' => $this->user->name]);
                }

                if ($user->level <= 2){

                    if($k == 0 ){//小于二级且为直推  判断是否为第一次购买产品  如果是第一次  赠送直推优惠券

                        if (firstGive($this->user->id,$user->id)){
                            //获得奖励发送消息
                            $service->sendSms($user->phone,SmsService::ORDER_CODE,['code' => $this->user->name]);
                        }
                    }

                }

            }
            $this->order->save();


        }catch (\Exception $e){
            dump($e->getMessage());
            $log['msg']     = $e->getMessage();
            $log['params']  = $this->user;
            $log['content'] = $e;
            ErrorLog::create($log);
        }
    }

    public function failed(\Exception $exception)
    {
        dump($exception->getMessage());
        $log['msg']     = $exception->getMessage();
        $log['params']  = $this->user;
        $log['content'] = $exception;
        ErrorLog::create($log);
    }
}
